package com.dfch.auto.appium.app;

import com.dfch.auto.appium.utils.TextUtils;
import io.appium.java_client.MobileBy;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.android.AndroidKeyCode;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

/**
 * @author ldcmaxim on 05/04/2018.
 */
public class AutoiOSDouYin extends AutoAndroid {

    private static final String APP_TAG = "douyin";
    private static final String APP_NAME = "抖音";

    public AutoiOSDouYin() {
    }

    @Override
    public boolean init(String deviceName, String platformVersion, String url) {
        DesiredCapabilities cap = getCapabilities();
        // Android system version
        cap.setCapability(MobileCapabilityType.PLATFORM_VERSION, platformVersion);
        // Adb device name
        cap.setCapability(MobileCapabilityType.DEVICE_NAME, deviceName);
        // Application package name
        cap.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "com.ss.android.ugc.aweme");
        // Application activity name
        cap.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, "com.ss.android.ugc.aweme.splash.SplashActivity");
        return super.connect(url, cap);
    }

    @Override
    public boolean execute(String script, int times) {
        if (TextUtils.isEmpty(script) || times <= 0 || !isConnected()) {
            return false;
        }
        String comment = script;
        AndroidDriver<AndroidElement> driver = getDriver();
        try {
            while (times > 0) {
                // 等待视频加载
                Thread.sleep(3000);

                try {
                    // 点击评论按钮
                    MobileElement showComment = driver.findElement(MobileBy.id("com.ss.android.ugc.aweme:id/lq"));
                    showComment.click();
                    Thread.sleep(200);
                } catch (Exception e) {
                    continue;
                }

                try {
                    // 点击评论输入框
                    MobileElement showCommentInput = driver.findElement(MobileBy.id("com.ss.android.ugc.aweme:id/lr"));
                    showCommentInput.click();
                    showCommentInput.sendKeys(comment);
                    Thread.sleep(500);
                } catch (Exception e) {
                    // 返回主页
                    driver.pressKeyCode(AndroidKeyCode.KEYCODE_BACK);
                    continue;
                }

                try {
                    // 获取发送按钮
                    MobileElement send = driver.findElement(MobileBy.id("com.ss.android.ugc.aweme:id/lt"));
                    send.click();
                    Thread.sleep(2000);
                } catch (Exception e) {
                    // 两次返回主页
                    driver.pressKeyCode(AndroidKeyCode.KEYCODE_BACK);
                    Thread.sleep(200);
                    driver.pressKeyCode(AndroidKeyCode.KEYCODE_BACK);
                    continue;
                }

                // 返回主页
                driver.pressKeyCode(AndroidKeyCode.KEYCODE_BACK);

                Thread.sleep(200);

                // 切换下一个视频
                SwipeUp(driver);

                // 执行完一次
                times--;
            }
        } catch (Exception e) {
            release();
        }
        return true;
    }

    @Override
    public void release() {
        super.release();
    }

    @Override
    public String getAppTag() {
        return APP_TAG;
    }

    @Override
    public String getAppName() {
        return APP_NAME;
    }
}
